package com.trailer.base.utils;

import android.app.Instrumentation;
import android.content.Intent;
import android.os.Build;
import android.view.KeyEvent;

import com.trailer.base.base.BApp;

public class KeySimulateUtils {

    public static void simulate(int keyCode) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            BApp.instance().startActivity(intent);
        } else if (keyCode == 279) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                runKeyCode(keyCode);
            }
        } else {
            runKeyCode(keyCode);
        }
    }


    private static void runKeyCode(int keyCode) {
        new Thread(() -> {
            Instrumentation inst = new Instrumentation();
            inst.sendKeyDownUpSync(keyCode);
        }).start();
    }

}
